﻿Imports System.Security.Cryptography
Imports System.Text

Public Class cls_encription

    Public Function Convert(ByVal str As String, ByVal ky As String) As String
        Try
            Dim kl As Integer
            Dim i As Integer
            Dim j As Integer
            Dim e As Integer

            If ky = "" Then
                Convert = str
                Exit Function
            End If

            'If ky.Length < 8 Then
            '    Convert = str
            '    Exit Function
            'End If


            kl = IIf((ky.Length < 32), ky.Length, 32)

            Dim k(0 To kl) As String
            For i = 0 To kl - 1
                k(i) = Asc(ky.Substring(i, 1)) And &H1F
            Next

            j = 0
            For i = 0 To str.Length - 1
                e = Asc(str.Substring(i, 1))
                str = set_character(str, IIf(e And &HE0, Chr(e Xor k(j)), Chr(e)), i)
                j = j + 1
                j = IIf(j = kl, 0, j)
            Next

            Convert = str

        Catch ex As Exception

            Convert = ""
        End Try

    End Function

    Private Function set_character(ByVal str As String, ByVal car As Char, ByVal pos As Integer) As String
        Dim vChar(str.Length) As Char
        vChar = str.ToCharArray
        vChar(pos) = car

        set_character = CStr(vChar)
    End Function

    Public Function HexToString(ByVal HexToStr As String) As String
        Dim strTemp As String
        Dim strReturn As String = vbNullString
        Dim I As Long
        For I = 1 To Len(HexToStr) Step 2
            strTemp = Chr(Val("&H" & Mid$(HexToStr, I, 2)))
            strReturn = strReturn & strTemp
        Next I
        HexToString = strReturn
    End Function

    Public Function StringToHex(ByVal StrToHex As String) As String
        Dim strTemp As String
        Dim strReturn As String = vbNullString
        Dim I As Long
        For I = 1 To Len(StrToHex)
            strTemp = Hex$(Asc(Mid$(StrToHex, I, 1)))
            If Len(strTemp) = 1 Then strTemp = "0" & strTemp
            strReturn = strReturn & strTemp
        Next I
        StringToHex = strReturn
    End Function

    'Shared Sub Main(ByVal args() As String)
    '    Dim [source] As String = "Hello World!"
    '    Using md5Hash As MD5 = MD5.Create()

    '        Dim hash As String = GetMd5Hash(md5Hash, source)

    '        Console.WriteLine("The MD5 hash of " + source + " is: " + hash + ".")

    '        Console.WriteLine("Verifying the hash...")

    '        If VerifyMd5Hash(md5Hash, [source], hash) Then
    '            Console.WriteLine("The hashes are the same.")
    '        Else
    '            Console.WriteLine("The hashes are not same.")
    '        End If
    '    End Using
    'End Sub 'Main



    Public Function GetMD5Hash(ByVal input As String) As String

        Dim md5Hash As MD5 = MD5.Create()

        ' Convert the input string to a byte array and compute the hash.
        Dim data As Byte() = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input))

        ' Create a new Stringbuilder to collect the bytes
        ' and create a string.
        Dim sBuilder As New StringBuilder()

        ' Loop through each byte of the hashed data 
        ' and format each one as a hexadecimal string.
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i

        ' Return the hexadecimal string.
        Return sBuilder.ToString()

    End Function 'GetMd5Hash


    ' Verify a hash against a string.
    'Public Function VerifyMd5Hash(ByVal md5Hash As MD5, ByVal input As String, ByVal hash As String) As Boolean
    '    ' Hash the input.
    '    Dim hashOfInput As String = GetMd5Hash(md5Hash, input)

    '    ' Create a StringComparer an compare the hashes.
    '    Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase

    '    If 0 = comparer.Compare(hashOfInput, hash) Then
    '        Return True
    '    Else
    '        Return False
    '    End If

    'End Function 'VerifyMd5Hash
End Class
